package core.proto.annotation;

import static app.constants.CollabVizConstants.*;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.apache.commons.io.FilenameUtils;

import core.proto.AllComponents;
import core.proto.Util;

/**
 * 
 * @author Wei
 *
 */
final class WaypointImageManager {

	private static HashMap<String, ImageIcon> filenameToWaypointImgMap;
	private static ImageIcon defaultImage; // TODO load the standard waypoint into this as well. 

	static{
		filenameToWaypointImgMap = new HashMap<String, ImageIcon>();


		HashMap<String, ImageIcon> tmpPngFileNameToImageMap = new HashMap<String, ImageIcon>(); 
		// Open the resource file. 
		// TODO need better designed resource retrieval mechanism.
		// TODOwei rewrite this confusing logic.
		BufferedReader reader;

		try {
			// Read the mapping file line by line. 
			// Then read the appropriate image (cache it if it was never read before), and store the mapping. 
			InputStream mappingFile = Util.getServerResourceAsStream(DOC_WAYPOINTIMG_MAP_FILE);
			reader = new BufferedReader(new InputStreamReader(mappingFile));
			String line;

			while ((line = reader.readLine()) != null){
				line = line.trim();
				if (! line.equals("")){
					String[] tokens = line.split(COMMA);
					String fileName = FilenameUtils.getBaseName(tokens[0]);
					String imgFileName = tokens[1];

					if (tmpPngFileNameToImageMap.containsKey(imgFileName)) {
						filenameToWaypointImgMap.put(fileName, tmpPngFileNameToImageMap.get(imgFileName));
					} else {
						URL imageURL = Util.getServerResource(FilenameUtils.concat(IMG_FOLDER, imgFileName));
						ImageIcon img = new ImageIcon(imageURL);
						tmpPngFileNameToImageMap.put(imgFileName, img);
						filenameToWaypointImgMap.put(fileName, img);
					}
				}
			}
			defaultImage = new ImageIcon(Util.getServerResource(FilenameUtils.concat(IMG_FOLDER, DEFAULT_WAYPOINT_PNG)));
		} catch (IOException e) {
			throw new Error(e);
		}
	}

	/**
	 * This returns a waypoint image that matches 
	 * @return
	 */
	public static ImageIcon getMatchingColorWaypoint(){
		String currentDoc = AllComponents.getCurrentPath();
		if (currentDoc == null || currentDoc.equals(NO_DOC_OPEN_TAG)){
			return defaultImage;
		} else {
			currentDoc = FilenameUtils.getBaseName(currentDoc);
			if (filenameToWaypointImgMap.containsKey(currentDoc)){
				return filenameToWaypointImgMap.get(currentDoc);
			} else {
				System.err.println("Error: no waypoint image specified, check mapping file for doc=" + currentDoc);
				return defaultImage;
			}
		}
	}
}
